#!/usr/bin/env python3
"""
MongoDB Toolchain configuration generator for DevContainers.

Generates toolchain_config.env with URLs and SHA256 checksums for both ARM64 and AMD64.
"""

import os
import sys
import tempfile
from datetime import datetime

# Add script directory to path for importing local modules
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from s3_artifact_utils import calculate_sha256, download_file, list_s3_objects

# S3 bucket and prefix
BUCKET = "boxes.10gen.com"
PREFIX = "build/toolchain/mongodbtoolchain-ubuntu2404"


def fetch_toolchain_info(arch: str) -> dict:
    """Fetch toolchain info for a specific architecture."""
    print(f"\n🔍 Fetching {arch} toolchain...", file=sys.stderr)

    # Use path-style URL because bucket name contains dots (boxes.10gen.com)
    # amd64 toolchains don't have an architecture suffix, so we need to exclude arm64
    if arch == "amd64":
        arch_prefix = PREFIX
        exclude_pattern = "-arm64-"
    else:
        arch_prefix = f"{PREFIX}-{arch}"
        exclude_pattern = None

    objects = list_s3_objects(BUCKET, arch_prefix, path_style=True)

    # Filter out excluded patterns
    if exclude_pattern:
        objects = [obj for obj in objects if exclude_pattern not in obj["Key"]]

    if not objects:
        print(f"No artifacts found with prefix: {arch_prefix}", file=sys.stderr)
        sys.exit(1)

    latest = max(objects, key=lambda x: x["LastModified"])
    url = f"https://s3.amazonaws.com/{BUCKET}/{latest['Key']}"
    key = latest["Key"]
    last_modified = latest["LastModified"]

    print(f"Latest: {key}", file=sys.stderr)
    print(f"Modified: {last_modified}", file=sys.stderr)

    # Download to temp location to calculate checksum
    with tempfile.NamedTemporaryFile(delete=False, suffix=".tar.gz") as tmp:
        tmp_path = tmp.name

    try:
        download_file(url, tmp_path)
        sha256 = calculate_sha256(tmp_path)
        print(f"SHA256: {sha256}", file=sys.stderr)
    finally:
        os.unlink(tmp_path)

    toolchain_id = os.path.basename(key).replace(".tar.gz", "")

    return {
        "url": url,
        "sha256": sha256,
        "key": key,
        "last_modified": last_modified,
        "toolchain_id": toolchain_id,
    }


def main():
    """Generate toolchain configuration file."""
    # Fetch both architectures
    arm64_info = fetch_toolchain_info("arm64")
    amd64_info = fetch_toolchain_info("amd64")

    # Determine output path
    script_dir = os.path.dirname(os.path.abspath(__file__))
    output_file = os.path.join(script_dir, "toolchain_config.env")

    # Write config file
    with open(output_file, "w") as f:
        f.write("# Generated by toolchain.py\n")
        f.write("# DO NOT EDIT MANUALLY - run: python3 toolchain.py\n")
        f.write("#\n")
        f.write(f"# Generated: {datetime.now().isoformat()}\n")
        f.write("\n")
        f.write("# ARM64 Toolchain\n")
        f.write(f"# Last Modified: {arm64_info['last_modified'].isoformat()}\n")
        f.write(f"# Toolchain ID: {arm64_info['toolchain_id']}\n")
        f.write(f'TOOLCHAIN_ARM64_URL="{arm64_info["url"]}"\n')
        f.write(f'TOOLCHAIN_ARM64_SHA256="{arm64_info["sha256"]}"\n')
        f.write(f'TOOLCHAIN_ARM64_KEY="{arm64_info["key"]}"\n')
        f.write(f'TOOLCHAIN_ARM64_LAST_MODIFIED="{arm64_info["last_modified"].isoformat()}"\n')
        f.write("\n")
        f.write("# AMD64 Toolchain\n")
        f.write(f"# Last Modified: {amd64_info['last_modified'].isoformat()}\n")
        f.write(f"# Toolchain ID: {amd64_info['toolchain_id']}\n")
        f.write(f'TOOLCHAIN_AMD64_URL="{amd64_info["url"]}"\n')
        f.write(f'TOOLCHAIN_AMD64_SHA256="{amd64_info["sha256"]}"\n')
        f.write(f'TOOLCHAIN_AMD64_KEY="{amd64_info["key"]}"\n')
        f.write(f'TOOLCHAIN_AMD64_LAST_MODIFIED="{amd64_info["last_modified"].isoformat()}"\n')

    print(f"\n✅ Configuration written to: {output_file}", file=sys.stderr)
    print("\nContents:", file=sys.stderr)
    with open(output_file) as f:
        print(f.read(), file=sys.stderr)


if __name__ == "__main__":
    main()
